﻿
using System;
using System.Collections.Generic;
using System.Text;

namespace FlashPay.Service.Impl
{
    using FlashPay.DAO.Interface;
    using FlashPay.EF.Models;
    using FlashPay.Entity;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Response.Company;
    using FlashPay.Entity.Response.Payment;
    using FlashPay.Service.Interface;
    using FlashPay.Util;
    using System.Linq;

    /// <summary>
    /// 余额变化业层接口实现
    /// </summary>
    public class AdjustBalanceServiceImpl : AdjustBalanceService
    {
        #region 注入
        /// <summary>
        /// 余额变化数据接口
        /// </summary>
        private readonly AdjustBalanceDao _adjustBalanceDao;

        /// <summary>
        /// 公司数据接口
        /// </summary>
        private readonly CompanyDao _companyDao;

        /// <summary>
        /// 银行数据接口
        /// </summary>
        private readonly BankDao _bankDao;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="adjustBalanceDao">余额变化数据接口</param>
        public AdjustBalanceServiceImpl(AdjustBalanceDao adjustBalanceDao, CompanyDao companyDao, BankDao bankDao)
        {
            this._adjustBalanceDao = adjustBalanceDao;
            this._companyDao = companyDao;
            this._bankDao = bankDao;
        }
        #endregion

        /// <summary>
        /// 根据编号获取余额变化记录
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>Company</returns>
        public AdjustBalance Get(long id) {
            return _adjustBalanceDao.Get(id);
        }

        /// <summary>
        /// 根据编号获取公司
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>Company</returns>
        private List<CompanyResponse> GetCompany(int companyId, int level)
        {
            var companyRequest = new EF.Models.Company()
            {
                CompanyId = companyId,
                CompanyPid = -1,
                CompanyStatus = 1
            };

            var companyList = new BaseModel<List<EF.Models.Company>>();

            _companyDao.Get(companyList, companyRequest, level);
            var companyResponse = new List<CompanyResponse>();
            if (companyList.Success)
            {
                foreach (var item in companyList.Result)
                {
                    companyResponse.Add(new CompanyResponse()
                    {
                        CompanyID = item.CompanyId,
                        CompanyName = item.CompanyName,
                        CompanyStatus = item.CompanyStatus
                    });
                }
            }
            return companyResponse;
        }

        /// <summary>
        /// 付款卡余额变化查询
        /// </summary>
        /// <param name="query">参数</param>
        public DataGrid<PaymentAdjustBalanceResponse> GetPaymentAdjustBalancePager(PaymentAdjustBalanceQuery query)
        {
            var companys = _bankDao.GetCompany(query.CompanyId.Value);
            if (companys != null && companys.Any()) {
                if (!string.IsNullOrEmpty(query.CompanyName))
                {
                    query.CompanyIds = companys.Where(p => p.CompanyName.Contains(query.CompanyName)).Select(p => p.CompanyID).ToList();
                }
                else {
                    query.CompanyIds = companys.Select(p => p.CompanyID).ToList();
                }
            }
            
            return _adjustBalanceDao.GetPaymentAdjustBalancePager(query);
        }

        /// <summary>
        /// 收、付、中款卡余额变化查询导出
        /// </summary>
        /// <param name="query">参数</param>
        public List<PaymentAdjustBalanceResponse> GetPaymentAdjustBalanceExport(PaymentAdjustBalanceQuery query)
        {
            var companys = _bankDao.GetCompany(query.CompanyId.Value);
            if (companys != null && companys.Any())
            {
                if (!string.IsNullOrEmpty(query.CompanyName))
                {
                    query.CompanyIds = companys.Where(p => p.CompanyName.Contains(query.CompanyName)).Select(p => p.CompanyID).ToList();
                }
                else
                {
                    query.CompanyIds = companys.Select(p => p.CompanyID).ToList();
                }
            }

            return _adjustBalanceDao.GetPaymentAdjustBalanceExport(query);
        }
    }
}
